#include"Queue.h"

void QueneInit(Que* ps)
{
	assert(ps);
	ps->head = ps->tail = NULL;
	ps->size = 0;
}

void Destroy(Que* ps)
{
	assert(ps);
	QNode* cur = ps->head;
	while (cur)
	{
		QNode*next = cur->next;
		free(cur);
		cur = next;
	}

	ps->head = ps->tail = NULL;
	ps->size = 0;
}

void QueuePush(Que* ps, QDateType x)
{
	assert(ps);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->date = x;
	newnode->next = NULL;

	if (ps->tail == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
	ps->size++;
}

void QueuePop(Que* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	if (ps->head->next == NULL)
	{
		free(ps->head);
		ps->head = ps->tail = NULL;

	}
	else 
	{
		Que* next = ps->head->next;
		free(ps->head);
		ps->head = next;
	}
	ps->size--;
}

QDateType QueueFront(Que* ps)
{
	assert(ps);

	return ps->head->date;
}


QDateType QueueBack(Que* ps)
{
	assert(ps);
	
	return ps->tail->date;
}


bool QueueEmpty(Que* ps)
{
	assert(ps);
	
	return ps->head == NULL;
}